package cc.catface.common.fastdfs;

import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public class UploadMediaUtil {
    public static String upload(MultipartFile file, FastFileStorageClient fastFileStorageClient) throws IOException {
        String path = "";
        if (file != null) {
            // 获取文件上传的文件名称
            String fileName = file.getOriginalFilename();
            if (StringUtils.isNotBlank(fileName)) {
                String[] fileNameArr = fileName.split("\\.");

                // 获取文件的后缀名
                String suffix = fileNameArr[fileNameArr.length - 1];
                path = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), suffix, null).getFullPath();
                System.out.println(path);
            }

            return path;
        } else {
            return "文件不能为空! ";
        }
    }

}
